package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Car;
import com.ruoyi.system.service.IExcelService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ExcelService业务层处理
 *
 * @author 蒼飞
 * @date 2023-11-27
 */
@Service
public class ExcelServiceImpl implements IExcelService {
    /**
     * 上传excel并处理
     * @param fileInputStream
     * @return
     */
    @Value("${excel.name.carName}")
    String carName;
    @Value("${excel.name.carType}")
    String carType;
    @Override
    public List<Car> uploadExcel(FileInputStream fileInputStream) throws IOException {
        try{
            //创建车辆集合 方便存入数据库
            List<Car> cars = new ArrayList<>();
            //读取流中的工作簿
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            //获取工作表
            Sheet sheet = workbook.getSheetAt(0);
            //获取总行数
            int rowCount = sheet.getPhysicalNumberOfRows();
            int numberNum = 0,typeNum = 0;
            for (int rowNum = 0; rowNum < rowCount; rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (row != null ){
                    //第一行有多少列
                    int cellCount = row.getPhysicalNumberOfCells();
                    Car car = new Car();
                    for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                        Cell cell = row.getCell(cellNum);
                        if(rowNum == 0){
                            if (cell.toString().equals(carName)){
                                numberNum = cellNum;
                            }
                            if (cell.toString().equals(carType)){
                                typeNum = cellNum;
                            }
                            continue;
                        }
                        if (cell != null){
                            if(cellNum == numberNum){
                                cell.setCellType(CellType.STRING);
                                //System.out.println("车号:"+ cell.toString());
                                car.setNumber(cell.toString());
                            }
                            if(cellNum == typeNum){
                                //System.out.println("车型:"+cell.getStringCellValue());
                                car.setType(cell.getStringCellValue());
                            }
                        }
                    }
                    cars.add(car);
                }
            }
            //System.out.println("保存成功");
            //移除第一个多余的
            cars.remove(0);
            return cars;
        }catch (Exception e){
            throw new RuntimeException();
        }finally {
            fileInputStream.close();
        }
    }
}
